home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / Cache / Container / phplib.php < prev    next >
Encoding:
PHP Script  |  2004-10-01  |  9.3 KB  |  259 lines

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PEAR :: Cache                                                        |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997-2003 The PHP Group                                |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 2.0 of the PHP license,       |
  8. // | that is bundled with this package in the file LICENSE, and is        |
  9. // | available at through the world-wide-web at                           |
  10. // | http://www.php.net/license/2_02.txt.                                 |
  11. // | If you did not receive a copy of the PHP license and are unable to   |
  12. // | obtain it through the world-wide-web, please send a note to          |
  13. // | license@php.net so we can mail you a copy immediately.               |
  14. // +----------------------------------------------------------------------+
  15. // | Authors: Ulf Wendel <ulf.wendel@phpdoc.de>                           |
  16. // |          Sebastian Bergmann <sb@sebastian-bergmann.de>               |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: phplib.php,v 1.3 2003/01/04 11:54:46 mj Exp $
  20.  
  21. require_once 'Cache/Container.php';
  22.  
  23. /**
  24. * Stores cache data into a database table using PHPLibs DB abstraction.
  25. *
  26. * WARNING: Other systems might or might not support certain datatypes of 
  27. * the tables shown. As far as I know there's no large binary 
  28. * type in SQL-92 or SQL-99. Postgres seems to lack any 
  29. * BLOB or TEXT type, for MS-SQL you could use IMAGE, don't know 
  30. * about other databases. Please add sugestions for other databases to 
  31. * the inline docs.
  32. *
  33. * The field 'changed' is used by the garbage collection. Depending on 
  34. * your databasesystem you might have to subclass fetch() and garbageCollection().
  35. *
  36. * For _MySQL_ you need this DB table:
  37. *
  38. * CREATE TABLE cache (
  39. *   id          CHAR(32) NOT NULL DEFAULT '',
  40. *   cachegroup  VARCHAR(127) NOT NULL DEFAULT '',
  41. *   cachedata   BLOB NOT NULL DEFAULT '',
  42. *   userdata    VARCHAR(255) NOT NULL DEFAUL '',
  43. *   expires     INT(9) NOT NULL DEFAULT 0,
  44. *  
  45. *   changed     TIMESTAMP(14) NOT NULL,
  46. *  
  47. *   INDEX (expires),
  48. *   PRIMARY KEY (id, cachegroup)
  49. * )
  50. *
  51. * @author   Ulf Wendel  <ulf.wendel@phpdoc.de>, Sebastian Bergmann <sb@sebastian-bergmann.de>
  52. * @version  $Id: phplib.php,v 1.3 2003/01/04 11:54:46 mj Exp $
  53. * @package  Cache
  54. * @see      save()
  55. */
  56. class Cache_Container_phplib extends Cache_Container {
  57.   
  58.     /**
  59.     * Name of the DB table to store caching data
  60.     * 
  61.     * @see  Cache_Container_file::$filename_prefix
  62.     */  
  63.     var $cache_table = 'cache';
  64.  
  65.     /**
  66.     * PHPLib object
  67.     * 
  68.     * @var  object PEAR_DB
  69.     */
  70.     var $db;
  71.  
  72.     /**
  73.     * Name of the PHPLib DB class to use
  74.     * 
  75.     * @var  string  
  76.     * @see  $db_path, $local_path
  77.     */
  78.     var $db_class = '';
  79.  
  80.     /**
  81.     * Filename of your local.inc
  82.     * 
  83.     * If empty, 'local.inc' is assumed.
  84.     *
  85.     * @var       string
  86.     */
  87.     var $local_file = '';
  88.  
  89.     /**
  90.     * Include path for you local.inc
  91.     *
  92.     * HINT: If your're not using PHPLib's prepend.php you must 
  93.     * take care that all classes (files) references by you 
  94.     * local.inc are included automatically. So you might 
  95.     * want to write a new local2.inc that only referrs to 
  96.     * the database class (file) you're using and includes all required files.
  97.     *
  98.     * @var  string  path to your local.inc - make sure to add a trailing slash
  99.     * @see  $local_file
  100.     */
  101.     var $local_path = '';
  102.  
  103.     /**
  104.     * Creates an instance of a phplib db class to use it for storage.
  105.     *
  106.     * @param    mixed   If empty the object tries to used the 
  107.     *                   preconfigured class variables. If given it 
  108.     *                   must be an array with:
  109.     *                     db_class => name of the DB class to use
  110.     *                   optional:
  111.     *                     db_file => filename of the DB class
  112.     *                     db_path => path to the DB class
  113.     *                     local_file => kind of local.inc
  114.     *                     local_patk => path to the local.inc
  115.     *                   see $local_path for some hints.s
  116.     * @see  $local_path
  117.     */
  118.     function Cache_Container_phplib($options = '') {
  119.         if (is_array($options))
  120.             $this->setOptions($options,  array_merge($this->allowed_options, array('db_class', 'db_file', 'db_path', 'local_file', 'local_path')));
  121.  
  122.         if (!$this->db_class)
  123.             return new Cache_Error('No database class specified.', __FILE__, __LINE__);
  124.  
  125.         // include the required files
  126.         if ($this->db_file)
  127.             include_once($this->db_path . $this->db_file);
  128.  
  129.         if ($this->local_file)
  130.             include_once($this->local_path . $this->local_file);
  131.  
  132.         // create a db object 
  133.         $this->db = new $this->db_class;
  134.     } // end constructor
  135.  
  136.     function fetch($id, $group) {
  137.         $query = sprintf("SELECT expires, cachedata, userdata FROM %s WHERE id = '%s' AND cachegroup = '%s'",
  138.                             $this->cache_table, 
  139.                             $id,
  140.                             $group
  141.                          );
  142.         $this->db->query($query);
  143.         if (!$this->db->Next_Record())
  144.             return array(NULL, NULL, NULL);
  145.  
  146.         // last used required by the garbage collection   
  147.         // WARNING: might be MySQL specific         
  148.         $query = sprintf("UPDATE %s SET changed = (NOW() + 0) WHERE id = '%s' AND cachegroup = '%s'",
  149.                             $this->cache_table,
  150.                             $id,
  151.                             $group
  152.                           );
  153.         $this->db->query($query);
  154.         return array($this->db->f('expires'), $this->decode($this->db->f('cachedata')), $this->db->f('userdata'));
  155.     } // end func fetch
  156.  
  157.     /**
  158.     * Stores a dataset.
  159.     * 
  160.     * WARNING: we use the SQL command REPLACE INTO this might be 
  161.     * MySQL specific. As MySQL is very popular the method should
  162.     * work fine for 95% of you.
  163.     */
  164.     function save($id, $data, $expires, $group) {
  165.         $this->flushPreload($id, $group);
  166.  
  167.         $query = sprintf("REPLACE INTO %s (cachedata, expires, id, cachegroup) VALUES ('%s', %d, '%s', '%s')",
  168.                             $this->cache_table,
  169.                             addslashes($this->encode($data)),
  170.                             $this->getExpiresAbsolute($expires) ,
  171.                             $id,
  172.                             $group
  173.                          );
  174.         $this->db->query($query);
  175.  
  176.         return (boolean)$this->db->affected_rows(); 
  177.     } // end func save
  178.  
  179.     function remove($id, $group) {
  180.         $this->flushPreload($id, $group);
  181.         $this->db->query(
  182.                         sprintf("DELETE FROM %s WHERE id = '%s' AND cachegroup = '%s'",
  183.                             $this->cache_table,
  184.                             $id,
  185.                             $group
  186.                           )
  187.                     );
  188.  
  189.         return (boolean)$this->db->affected_rows();
  190.     } // end func remove
  191.  
  192.     function flush($group) {
  193.         $this->flushPreload();
  194.  
  195.         if ($group) {
  196.             $this->db->query(sprintf("DELETE FROM %s WHERE cachegroup = '%s'", $this->cache_table, $group));    
  197.         } else {
  198.             $this->db->query(sprintf("DELETE FROM %s", $this->cache_table));    
  199.         }
  200.  
  201.         return $this->db->affected_rows();
  202.     } // end func flush
  203.  
  204.     function idExists($id, $group) {
  205.         $this->db->query(
  206.                         sprintf("SELECT id FROM %s WHERE ID = '%s' AND cachegroup = '%s'", 
  207.                             $this->cache_table,
  208.                             $id, 
  209.                             $group
  210.                         )   
  211.                     );
  212.  
  213.         return (boolean)$this->db->nf();                         
  214.     } // end func isExists
  215.  
  216.     function garbageCollection($maxlifetime) {
  217.         $this->flushPreload();
  218.  
  219.         $this->db->query( 
  220.                         sprintf("DELETE FROM %s WHERE (expires <= %d AND expires > 0) OR changed <= (NOW() - %d)",
  221.                             $this->cache_table, 
  222.                             time(),
  223.                             $maxlifetime
  224.                         )
  225.                     );
  226.  
  227.         //check for total size of cache
  228.         $query = sprintf('select sum(length(cachedata)) as CacheSize from %s',
  229.                          $this->cache_table
  230.                        );
  231.  
  232.         $this->db->query($query);
  233.         $this->db->Next_Record();
  234.         $cachesize = $this->db->f('CacheSize');
  235.         //if cache is to big.
  236.         if ($cachesize > $this->highwater)
  237.         {
  238.             //find the lowwater mark.
  239.             $query = sprintf('select length(cachedata) as size, changed from %s order by changed DESC',
  240.                                      $this->cache_table
  241.                        );
  242.             $this->db->query($query);
  243.  
  244.             $keep_size=0;
  245.             while ($keep_size < $this->lowwater && $this->db->Next_Record() )
  246.             {
  247.                 $keep_size += $this->db->f('size');
  248.             }
  249.             //delete all entries, which were changed before the "lowwwater mark"
  250.             $query = sprintf('delete from %s where changed <= '.$this->db->f('changed'),
  251.                                      $this->cache_table
  252.                                    );
  253.  
  254.             $this->db->query($query);
  255.         }
  256.     } // end func garbageCollection
  257. }
  258. ?>